package com.ddx.service.impl;

import com.ddx.common.util.AliyunVideoUtil;
import com.ddx.common.util.DateUtil;
import com.ddx.dao.*;
import com.ddx.pojo.*;
import com.ddx.service.ICommentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2019/1/13
 */
@Service
public class CommentServiceImpl implements ICommentService {
    private static Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);
    @Autowired
    ShortVideoCommentMapper shortVideoCommentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ShortVideoLikeMapper shortVideoLikeMapper;
    @Autowired
    ShortVideoMapper shortVideoMapper;
    @Autowired
    ShortVideoComplaintMapper shortVideoComplaintMapper;
    @Autowired
    ShortCommentLikeMapper shortCommentLikeMapper;
    @Value("${fastdfs.server}")
    String fastdfsServer;
    @Override
    public Map<String, Object> commentList(Integer videoId,String userId) {
        logger.info("视频评论列表videoId={}",videoId);
        Map<String, Object> map=new HashMap<>();
        List<ShortVideoComment> shortVideoComments=shortVideoCommentMapper.selectFirstByVideoId(videoId);
        List<Map<String,Object>> mapList=new ArrayList<>();
        for(ShortVideoComment shortVideoComment:shortVideoComments){
            Map<String,Object> videoMap=new HashMap<>();
            User user=userMapper.selectByPrimaryKey(shortVideoComment.getUserId());
            videoMap.put("id",shortVideoComment.getId());
            videoMap.put("userId",shortVideoComment.getUserId());
            if(StringUtils.isNotBlank(user.getHeadImage())) {
                if (user.getHeadImage().contains("http")) {
                    videoMap.put("imageUrl",user.getHeadImage());
                } else {
                    videoMap.put("imageUrl",fastdfsServer + user.getHeadImage());
                }
            }
            if(StringUtils.isNotBlank(userId)){
                ShortCommentLike record=new ShortCommentLike();
                record.setUserId(Integer.parseInt(userId));
                record.setCommentId(shortVideoComment.getId());
                List<ShortCommentLike> likeList=shortCommentLikeMapper.selectByRecord(record);
                if(likeList==null||likeList.size()==0){
                    videoMap.put("isLike",0);
                }else {
                    videoMap.put("isLike",1);
                }
            }else{
                videoMap.put("isLike",0);
            }
            videoMap.put("userName",user.getName());
            videoMap.put("content",shortVideoComment.getContent());
            videoMap.put("commentLikeNum",shortVideoComment.getCommentLikeNum());
            String releaseTime=DateUtil.converDate(shortVideoComment.getCreateTime());
            videoMap.put("releaseTime",releaseTime);
            ShortVideoComment record=new ShortVideoComment();
            record.setVideoId(videoId);
            record.setToId(shortVideoComment.getId());
            List<ShortVideoComment> two=shortVideoCommentMapper.selectByRecord(record);
            for(ShortVideoComment s:two){
                User twoUser=userMapper.selectByPrimaryKey(s.getUserId());
                s.setUserName(twoUser.getName());
                if(StringUtils.isNotBlank(user.getHeadImage())) {
                    if (user.getHeadImage().contains("http")) {
                        s.setImageUrl(user.getHeadImage());
                    } else {
                        s.setImageUrl(fastdfsServer + user.getHeadImage());
                    }
                }
            }
            videoMap.put("commentList",two);
            mapList.add(videoMap);
        }
        map.put("commentList",mapList);
        return map;
    }

    @Override
    public void uploadComment(ShortVideoComment shortVideoComment) throws Exception {
        logger.info("上传评论入参shortVideoComment={}",shortVideoComment);
        if(shortVideoComment.getToId()!=null){
            ShortVideoComment videoComment=shortVideoCommentMapper.selectByPrimaryKey(shortVideoComment.getToId());
            User user=userMapper.selectByPrimaryKey(videoComment.getUserId());
            shortVideoComment.setContent("@"+user.getName()+":"+shortVideoComment.getContent());
        }
        shortVideoComment.setCommentLikeNum(0);
        shortVideoCommentMapper.insertSelective(shortVideoComment);
    }

    @Override
    public void commentThumbsUp(ShortVideoComment shortVideoComment) throws Exception {
        logger.info("评论点赞入参shortVideoComment={}",shortVideoComment);
        ShortCommentLike record=new ShortCommentLike();
        record.setUserId(shortVideoComment.getUserId());
        record.setCommentId(shortVideoComment.getId());
        List<ShortCommentLike> likeList=shortCommentLikeMapper.selectByRecord(record);
        if(likeList==null||likeList.size()==0){
            ShortCommentLike shortCommentLike=new ShortCommentLike();
            shortCommentLike.setUserId(shortVideoComment.getUserId());
            shortCommentLike.setCommentId(shortVideoComment.getId());
            shortCommentLikeMapper.insertSelective(shortCommentLike);
            shortVideoCommentMapper.updateAddLikeNum(shortVideoComment.getId());
        }else{
            ShortCommentLike shortCommentLike=new ShortCommentLike();
            shortCommentLike.setUserId(shortVideoComment.getUserId());
            shortCommentLike.setCommentId(shortVideoComment.getId());
            shortCommentLikeMapper.deleteByRecord(shortCommentLike);
            shortVideoCommentMapper.updateSubLikeNum(shortVideoComment.getId());
        }
    }

    @Override
    @Transactional
    public void videoThumbsUp(ShortVideo shortVideo) throws Exception {
        logger.info("视频点赞点赞入参shortVideo={}",shortVideo);
        ShortVideoLike record=new ShortVideoLike();
        record.setUserId(shortVideo.getUserId());
        record.setVideoId(shortVideo.getId());
        List<ShortVideoLike> likeList=shortVideoLikeMapper.selectByRecord(record);
        if(likeList==null||likeList.size()==0){
            ShortVideoLike shortVideoLike=new ShortVideoLike();
            shortVideoLike.setUserId(shortVideo.getUserId());
            shortVideoLike.setVideoId(shortVideo.getId());
            shortVideoLikeMapper.insertSelective(shortVideoLike);
            shortVideoMapper.updateAddLikeNum(shortVideo.getId());
        }else{
            ShortVideoLike shortVideoLike=new ShortVideoLike();
            shortVideoLike.setUserId(shortVideo.getUserId());
            shortVideoLike.setVideoId(shortVideo.getId());
            shortVideoLikeMapper.deleteByRecord(shortVideoLike);
            shortVideoMapper.updateSubLikeNum(shortVideo.getId());
        }
    }

    @Override
    public Map<String, Object> newsRecord(Integer userId) throws Exception {
        Map<String, Object> map=new HashMap<>();
        List<ShortVideoLike> list=shortVideoLikeMapper.selectNewsRecord(userId);
        List<Map<String,Object>> maps=new ArrayList<>();
        for(ShortVideoLike shortVideoLike:list){
            Map<String,Object> news=new HashMap<>();
            User user=userMapper.selectByPrimaryKey(shortVideoLike.getUserId());
            ShortVideo shortVideo=shortVideoMapper.selectByPrimaryKey(shortVideoLike.getVideoId());
            Map<String,String> video= AliyunVideoUtil.getPlayInfo(shortVideo.getVideoId());
            if(video==null){
                continue;
            }
            news.put("videoId",shortVideoLike.getVideoId());
            news.put("coverURL",video.get("coverURL"));
            news.put("playURL",video.get("playURL"));
            news.put("date", DateUtil.format(shortVideoLike.getCreateTime(),"yyyy-MM-dd"));
            news.put("timestamp",shortVideoLike.getCreateTime().getTime());
            news.put("content",user.getName()+"赞了你的作品");
            if(user.getHeadImage().contains("http")){
                news.put("imageUrl",user.getHeadImage());
            }else{
                news.put("imageUrl",fastdfsServer+user.getHeadImage());
            }
            maps.add(news);
        }
        List<ShortVideoComment> shortVideoComments=shortVideoCommentMapper.selectNewsList(userId);
        for(ShortVideoComment shortVideoComment:shortVideoComments){
            Map<String,Object> news=new HashMap<>();
            User user=userMapper.selectByPrimaryKey(shortVideoComment.getUserId());
            ShortVideo shortVideo=shortVideoMapper.selectByPrimaryKey(shortVideoComment.getVideoId());
            Map<String,String> video= AliyunVideoUtil.getPlayInfo(shortVideo.getVideoId());
            if(video==null){
                continue;
            }
            news.put("videoId",shortVideoComment.getVideoId());
            news.put("coverURL",video.get("coverURL"));
            news.put("playURL",video.get("playURL"));
            news.put("date", DateUtil.format(shortVideoComment.getCreateTime(),"yyyy-MM-dd"));
            news.put("timestamp",shortVideoComment.getCreateTime().getTime());
            news.put("content",user.getName()+"评论了你的作品");
            news.put("imageUrl",fastdfsServer+user.getHeadImage());
            maps.add(news);
        }
        Collections.sort(maps, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return new BigDecimal(o1.get("timestamp").toString()).compareTo(new BigDecimal(o2.get("timestamp").toString()));
            }
        });
        map.put("newsList",maps);
        return map;
    }

    @Override
    public void complaint(ShortVideoComplaint shortVideoComplaint) throws Exception {
        logger.info("小视频举报入参{}",shortVideoComplaint);
        shortVideoComplaintMapper.insertSelective(shortVideoComplaint);
    }

    @Override
    public Map<String, Object> myComplaint(Integer userId) throws Exception {
        logger.info("我的小视频举报入参{}",userId);
        Map<String,Object> map=new HashMap<>();
        ShortVideoComplaint record=new ShortVideoComplaint();
        record.setUserId(userId);
        List<ShortVideoComplaint> list=shortVideoComplaintMapper.selectByRecord(record);
        for(ShortVideoComplaint shortVideoComplaint:list){
            shortVideoComplaint.setDate(DateUtil.format(shortVideoComplaint.getCreateTime(),"yyyy-MM-dd hh:mm"));
        }
        map.put("complaintList",list);
        return map;
    }
}
